<!--
*@Author: QWP
*@Description: 锚点
*@Date: 2023-06-29 17:32:47
-->
<template>
  <div class="u-anchor"
    id="u-anchor">
    <slot></slot>
  </div>
</template>
<script>
export default {
  name: 'u-anchor',
  props: {
    current: {
      type: String,
      default: ""
    },
  },
  data () {
    return {
      timer: null
    }
  },
  computed: {
  },
  mounted() {},
  methods: {
    position() {
      this.timer = setTimeout(() => {
        let dom = document.getElementById(this.current)
        if(dom) {
          dom.scrollIntoView({'behavior': "smooth"})
          clearTimeout(this.timer)
        }
        else {
          this.position()
        }
      }, 20)
    }
  },
  watch: {
    current:{
      handler: function(nval, ova) {
        if(nval != '') {
          this.position()
        }
      },
      immediate: true
    }
  }
}
</script>